package servlet;

import entity.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import service.NewsDaoService;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

@WebServlet("/articleDetails")
public class ArticleDetails extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        NewsDaoService nds = new NewsDaoService();
        HttpSession session=request.getSession();
        String savePath = request.getServletContext().getRealPath("/md");  //存储路径

        String newsId = request.getParameter("newsid");
        int links = Integer.parseInt(request.getParameter("links"));
        System.out.println(newsId);
        News details = nds.selectByNewsId(newsId);
        System.out.println(details.getNewsId());
        if(links == 0){
            String fileName = savePath+"/"+details.getNewsText();
            String s = Files.readString(Paths.get(fileName));
            details.setNewsText(s);
            session.setAttribute("details",details );
            response.sendRedirect("./WinterTourism/material/articleDetails.jsp");
        }else {
            response.sendRedirect(details.getNewsText());
        }


//        String fileName = savePath+"/"+file;
        // java 11 开始提供的方法，读取文件不能超过2G，与你的内存息息相关
        /*
         *
         *  byte[] bytes = Files.readAllBytes(Paths.get(fileName));
         * String content = new String(bytes, StandardCharsets.UTF_8);
         * System.out.println(content);*/


    }
}
